<template>
  <el-form ref="form" :model="form" :rules="rules" label-width="80px">
    <el-form-item label="商品货号" prop="sku">
      <el-input v-model="form.sku" placeholder="请输入商品货号" disabled/>
    </el-form-item>
    <el-form-item label="标题" prop="title">
      <el-input v-model="form.title" placeholder="请输入标题" />
    </el-form-item>
    <el-form-item label="副标题" prop="subTitle">
      <el-input v-model="form.subTitle" placeholder="请输入副标题" />
    </el-form-item>
    <el-form-item label="图片" prop="pic">
      <image-upload v-model="form.pic" :limit = "1" />
    </el-form-item>
    <el-form-item label="价格" prop="price">
      <el-input v-model="form.price" placeholder="请输入价格" />
    </el-form-item>
    <el-form-item label="关键字" prop="keywords">
      <el-input v-model="form.keywords" placeholder="请输入关键字" />
    </el-form-item>
    <el-form-item label="分类主键" prop="categoryId">
      <treeselect v-model="form.categoryId" :options="categoryOptions" :disable-branch-nodes="true" :normalizer="normalizer" placeholder="请选择父主键" />
    </el-form-item>
    <el-form-item label="详情内容">
      <editor v-model="form.detailHtml" :min-height="192"/>
    </el-form-item>
    <el-form-item label="画册图片" prop="albumPics">
      <image-upload v-model="form.albumPics"/>
    </el-form-item>
    <el-form-item label="排序" prop="sort">
      <el-input v-model="form.sort" placeholder="请输入排序" />
    </el-form-item>
  </el-form>
</template>

<script>
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

import { listCategory} from "@/api/supermarket/category";

export default {
  name: "EditProductBasic",
  components: {
    Treeselect
  },
  props: {
    form: {
      type: Object,
      default: null
    }
  },
  data() {
    return {

      // 分类树选项
      categoryOptions: [],

      rules: {
        title: [
          { required: true, message: "标题不能为空", trigger: "blur" }
        ],
        subTitle: [
          { required: true, message: "副标题不能为空", trigger: "blur" }
        ],
        price: [
          { required: true, message: "价格不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created () {
    //加载分类数据
    this.getCategoryTreeselect();
  },
  methods: {
    /** 查询分类下拉树结构 */
    getCategoryTreeselect() {
      listCategory().then(response => {
        this.categoryOptions = [];
        const data = { id: 0, name: '顶级节点', children: [] };
        data.children = this.handleTree(response.data, "id", "pId");
        this.categoryOptions.push(data);
      });
    },
    /** 转换分类数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.name,
        children: node.children
      };
    },
  }
};
</script>
